/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package construtora.limiar.dao;

import construtora.limiar.dao.generic.AbstractDAO;
import construtora.limiar.dao.generic.DAOEngine;
import construtora.limiar.dao.generic.IDAO;
import construtora.limiar.models.Funcionario;
import construtora.limiar.models.Setor;
import construtora.limiar.models.generic.IModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author michel
 */
public class SetorDAO  extends AbstractDAO implements IDAO{
    
    public SetorDAO(){
        this.table = "setor";
    }

    @Override
    public String[][] getFields(IModel model) {
        Setor setor= (Setor) model;
        String[][] fields = new String[3][2];
        fields[0][0] = "nome";
        fields[0][1] = "'" + setor.getNome() + "'";
        fields[1][0] = "id_funcionario_responsavel";
        fields[1][1] = "'" + setor.getFuncionarioResponsavel().getId().toString() + "'";
        fields[2][0] = "status";
        fields[2][1] = "'" + setor.getStatus() + "'";
        return fields;

    }

    @Override
    public IModel fetchOne(IModel model) {
        String where = this.getDefaultWhere(model);
        List<Setor> list = (List<Setor>) this.fetchAll(where, null, null, null);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return null;
    }

    @Override
    public List<? extends IModel> fetchAll(String where, String order, Integer limit, Integer offset) {
        List<Setor> list = new ArrayList<Setor>();
        ResultSet rs = DAOEngine.fetchAll(this.table, where, order, limit, offset);
        try {
            while (rs.next()){
                Setor setor = new Setor();
                FuncionarioDAO dao = new FuncionarioDAO();
                setor.setId(rs.getInt("id_setor"));
                setor.setNome(rs.getString("nome"));
                Funcionario funcionario = new Funcionario();
                funcionario.setId(rs.getInt("id_funcionario_responsavel"));
                setor.setFuncionarioResponsavel(funcionario);
                //setor.setFuncionarioResponsavel((Funcionario) dao.fetchOne(new Funcionario(rs.getInt("id_funcionario_responsavel"))));
                setor.setStatus(rs.getString("status"));
                list.add(setor);
                
            }
            return list;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
